package org.androidpn.adapter;

import java.util.List;

import org.androidpn.R;
import org.androidpn.activity.ShowPicActivity;
import org.androidpn.activity.UserInfoDetailActivity;
import org.androidpn.http.HttpApi;
import org.androidpn.model.CTalkList;
import org.androidpn.model.CTalkMessage;
import org.androidpn.model.CUserInfo;
import org.androidpn.model.NetModel;
import org.androidpn.utils.AppCache;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MessageListAdapter extends BaseAdapter {

	private Context context;
	private List<CTalkMessage> msgList;

	private CTalkList cTalkList;

	public CTalkList getcTalkList() {
		return cTalkList;
	}

	String mytelephone = "";
	String othertelephone;

	public void setcTalkList(CTalkList cTalkList) {
		if (cTalkList == null) {
			return;
		}
		this.cTalkList = cTalkList;
		if (this.cTalkList.getMy_telephone().equals(mytelephone)) {
			othertelephone = cTalkList.getFriend_telephone();
		} else {
			othertelephone = cTalkList.getMy_telephone();
		}

	}

	public MessageListAdapter(Context context) {
		this.context = context;
		mytelephone = AppCache.getInstance().getUseInfo().getTelephone();
	}

	public void setMsgList(List<CTalkMessage> msgList) {
		this.msgList = msgList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (msgList == null) {
			return 0;
		} else {
			return msgList.size();
		}
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return msgList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		CTalkMessage data = msgList.get(arg0);
		if (arg1 == null) {
			arg1 = View.inflate(context, R.layout.msg_list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.lyIn = (RelativeLayout) arg1.findViewById(R.id.ly_in);
			viewHolder.lyOut = (RelativeLayout) arg1.findViewById(R.id.ly_out_out);
			viewHolder.ivHeadIn = (ImageView) arg1.findViewById(R.id.iv_head_in);
			viewHolder.ivHeadOut = (ImageView) arg1.findViewById(R.id.iv_head_out);
			viewHolder.tvMsgin = (TextView) arg1.findViewById(R.id.tv_msg_in);
			viewHolder.tvMsgout = (TextView) arg1.findViewById(R.id.tv_msg_out);
			viewHolder.ivMsgin = (ImageView) arg1.findViewById(R.id.iv_msg_in);
			viewHolder.ivMsgout = (ImageView) arg1.findViewById(R.id.iv_msg_out);
			arg1.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) arg1.getTag();
		}
		viewHolder.ivMsgin.setOnClickListener(new MyOnClickListener(data));
		viewHolder.ivMsgout.setOnClickListener(new MyOnClickListener(data));
		DisplayImageOptions options1 = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).showImageOnFail(R.drawable.default_icon)
				.showImageForEmptyUri(R.drawable.default_icon).showImageOnLoading(R.drawable.default_icon).build();
		DisplayImageOptions options2 = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
				.considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).showImageOnFail(R.drawable.loading_defalt)
				.showImageForEmptyUri(R.drawable.loading_defalt).showImageOnLoading(R.drawable.loading_defalt).build();
		if (!data.getSender_telephone().equals(mytelephone)) {
			viewHolder.ivHeadIn.setOnClickListener(new IvHeadClickListener(othertelephone));
			viewHolder.lyIn.setVisibility(View.VISIBLE);
			viewHolder.lyOut.setVisibility(View.GONE);
			if (cTalkList != null) {
				if (othertelephone.equals(cTalkList.getFriend_telephone())) {
					ImageLoader.getInstance().displayImage(ConstanceUtil.HOST_NAME + cTalkList.getFriend_icon(),
							viewHolder.ivHeadIn, options1);
				} else {
					ImageLoader.getInstance().displayImage(ConstanceUtil.HOST_NAME + cTalkList.getMy_icon(),
							viewHolder.ivHeadIn, options1);
				}

			}
			if (data.getMessage_type() == ConstanceUtil.MESSAGETYPE_TEXT) {
				viewHolder.tvMsgin.setVisibility(View.VISIBLE);
				viewHolder.ivMsgin.setVisibility(View.GONE);
				viewHolder.tvMsgin.setText(data.getMessage_content());
			} else {
				viewHolder.tvMsgin.setVisibility(View.GONE);
				viewHolder.ivMsgin.setVisibility(View.VISIBLE);
				String imgurl = ConstanceUtil.HOST_NAME + data.getMessage_content();
				ImageLoader.getInstance().displayImage(imgurl, viewHolder.ivMsgin, options2);
			}
		} else {

			viewHolder.lyIn.setVisibility(View.GONE);
			viewHolder.lyOut.setVisibility(View.VISIBLE);
			viewHolder.ivHeadOut.setOnClickListener(new IvHeadClickListener(mytelephone));
			if (cTalkList != null) {
				if (mytelephone.equals(cTalkList.getFriend_telephone())) {
					ImageLoader.getInstance().displayImage(ConstanceUtil.HOST_NAME + cTalkList.getFriend_icon(),
							viewHolder.ivHeadOut, options1);
				} else {
					ImageLoader.getInstance().displayImage(ConstanceUtil.HOST_NAME + cTalkList.getMy_icon(),
							viewHolder.ivHeadOut, options1);
				}
				// ImageLoader.getInstance().displayImage(ConstanceUtil.HOST_NAME
				// + cTalkList.getMy_icon(),
				// viewHolder.ivHeadIn, options1);

			}
			if (data.getMessage_type() == ConstanceUtil.MESSAGETYPE_TEXT) {
				viewHolder.tvMsgout.setVisibility(View.VISIBLE);
				viewHolder.ivMsgout.setVisibility(View.GONE);
				viewHolder.tvMsgout.setText(data.getMessage_content());
			} else {
				viewHolder.tvMsgout.setVisibility(View.GONE);
				viewHolder.ivMsgout.setVisibility(View.VISIBLE);
				if (!msgList.get(arg0).isLocaldata) {
					String imgurl = ConstanceUtil.HOST_NAME + data.getMessage_content();
					ImageLoader.getInstance().displayImage(imgurl, viewHolder.ivMsgout, options2);
				} else {
					ImageLoader.getInstance().displayImage("file://" + data.getMessage_content(), viewHolder.ivMsgout);
				}
			}

		}

		return arg1;
	}

	class ViewHolder {
		RelativeLayout lyIn;
		RelativeLayout lyOut;

		ImageView ivHeadIn;
		ImageView ivHeadOut;

		TextView tvMsgin;
		TextView tvMsgout;

		ImageView ivMsgin;
		ImageView ivMsgout;
	}

	class MyOnClickListener implements OnClickListener {
		CTalkMessage cTalkMessage;

		public MyOnClickListener(CTalkMessage cTalkMessage) {
			this.cTalkMessage = cTalkMessage;
		}

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.iv_msg_in:
			case R.id.iv_msg_out:
				Intent intent = new Intent(context, ShowPicActivity.class);
				intent.putExtra("ctalkmessage", cTalkMessage);
				context.startActivity(intent);
				break;

			default:
				break;
			}
		}
	}

	class IvHeadClickListener implements OnClickListener {
		private String telephone;

		public IvHeadClickListener(String telephone) {
			this.telephone = telephone;
		}

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			GetUserinfoResponse getUserinfoResponse = new GetUserinfoResponse();
			HttpApi.getUserInfo(telephone, getUserinfoResponse, getUserinfoResponse);
		}

	}

	class GetUserinfoResponse implements Listener<String>, ErrorListener {

		@Override
		public void onErrorResponse(VolleyError error) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onResponse(String response) {
			// TODO Auto-generated method stub
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS)
				;
			{
				CUserInfo cUserInfo = JsonUtil.parseObject(netModel.getContent(), CUserInfo.class);
				Intent intent = new Intent(context, UserInfoDetailActivity.class);
				intent.putExtra("cuserinfo", cUserInfo);
				context.startActivity(intent);
			}
		}

	}
}
